import goods from '@/api/goods'

const state = {
    categories: [],
    goodsList: [],
    goodsInfo: {},
    goodsPic: [],
    commentInfo: '',
    commentList: [],
    // 指确定attr以后的各项数据
    goodsDetail: {},
    goodsId: '',
    goodsAttrs: [],
    collection: {
        page: 0,
        data: []
    },
    requestSend: {
        categories: false,
    }
}

const getters = {
    categories: state => state.categories,
    goodsList: state => state.goodsList,
    goodsInfo: state => state.goodsInfo,
    goodsDetail: state => state.goodsDetail,
    goodsAttrs: state => state.goodsAttrs,
    goodsPic: state => state.goodsPic,
    commentInfo: state => state.commentInfo,
    commentList: state => state.commentList,
    collection: state => state.collection.data,    
}

const actions = {
    // 获取分类
    async getCategories({state, commit}) {
        if(state.requestSend.categories) return
        const {data, success} = await goods.getCategories()
        commit('categories', data.CategoryInfo)
    },
    // 获取商品列表
    async getGoodsList({state, commit}, {classAppendId, classId, page}) {
        const {data} = await goods.getGoodsList({classAppendId, classId, page})
        commit('goodsList', data.goods_list)
        return data.goods_list
    },
    // 搜索商品
    async search({state, commit}, {search, page}) {
        const {data} = await goods.search({search, page})
        commit('goodsList', data.goods_list)
        return data.goods_list
    },
    // 获取商品描述及图片
    async getGoodsInfo({commit}, preGoodsId) {
        const res = await Promise.all([goods.getGoodsDetail(preGoodsId),
            goods.getGoodsDetailPic(preGoodsId)])
        commit('goodsInfo',{
            goodsInfo: res[0].data,
            goodsPic: res[1].data
        })
    },
    // 获取商品属性及可提供属性配对
    async getGoodsAttrs({commit}, preGoodsId) {
        const {data: attrs} = await goods.getGoodsAttrs(preGoodsId)
        // 给弹层设置初始值
        commit('goodsDetail',　{
            price: attrs.price,
            bonus: 0,
            pic: attrs.picture,
            stock: attrs.stock
        })
        const {data: matches} = await goods.getGoodsAttrsMatch(preGoodsId)
        commit('goodsAttrs',{
            attrs: attrs.attribute_info,
            matches
        })
    },
    // 选择属性后 获取goodsId及详细价格等
    async getGoodsId({commit}, a) {
        const {data} = await goods.getGoodsId(a)
        const goodsInfo = data.goods_info
        commit('goodsDetail', {
            price: goodsInfo.agent_price,
            bonus: goodsInfo.bonus,
            pic: goodsInfo.picture,
            stock: goodsInfo.stores,
            goodsId: goodsInfo.goods_id
        })
        return goodsInfo.goods_id
    },
    // 获取商品评论
    async getGoodsComments({state, commit}, a) {
        const {data} = await goods.getGoodsComments(a)
        commit('commentInfo', data)
        return data.comment_info
    },
    // 收藏/取消收藏
    async toToggleCollect({state, commit}, preGoodsId) {
        if(state.goodsInfo.pre_goods_info.is_collected == 1)
            await goods.uncollectGoods(preGoodsId)
        else await goods.collectGoods(preGoodsId)
        commit('toggleCollect')
    },
    // 获取收藏列表
    async getCollection({state, commit}, page) {
        console.log(page)
        console.log(state.collection.page)
        if(page <= state.collection.page) return []
        const {data, success, msg} = await goods.getCollection(page)
        commit('collection', data.list)
        return data.list
    }
}

const mutations = {
    categories(state, cate) {
        state.categories = cate
        state.requestSend.categories = true
    },
    goodsList(state, goodsList) {
        state.goodsList.push(...goodsList)
    },
    clearList(state) {
        state.goodsList = []
    },
    goodsInfo(state,{goodsInfo, goodsPic}) {
        state.goodsInfo = goodsInfo
        state.goodsPic = goodsPic.detail_pic
    },
    goodsDetail(state, {price, bonus, pic, stock, goodsId}) {
        state.goodsDetail = {
            price, bonus, pic, stock  
        }
        state.goodsId = goodsId
    },
    // 因为当前选择的商品保留在了这里 所以退出选择时务必要clear
    clearGoodsDetail(state) {
        state.goodsDetail = {}
        state.goodsId = ''
    },
    // 当前商品的goodsAttr也保留在了这里
    goodsAttrs(state, {attrs, matches}) {
        let attrArr = []
        attrs.forEach(a => {
            a.list.forEach(l => l.availableMatches = matches[l.attribute_id])
            attrArr.push(a)
        })
        state.goodsAttrs = attrArr
    },
    commentInfo(state, commentInfo) {
        state.commentInfo = commentInfo
        state.commentList.push(...commentInfo.comment_info)
    },
    clearComment(state) {
        state.commentList = []
    },
    toggleCollect(state) {
        state.goodsInfo.pre_goods_info.is_collected = !state.goodsInfo.pre_goods_info.is_collected
        // 需要操作state.collection.data 但是这里存在vuex push进一个新商品
        // 但是拉取下一页时又从数据库拉了一遍 这样子的问题!!
        // 移除已收藏商品貌似没问题
    },
    collection(state, collectionList) {
        state.collection.data.push(...collectionList)
        state.collection.page++
    }
}

export default {
    state,
    getters,
    actions,
    mutations,
    namespaced: true
  }